$(document).ready(function () {
    $.ajax({
        type : "get",
        url : "/register/getClazz",
        dataType : "json",
        success : function(data) {
            for(var i in data){
                if(i==0){
                    $("#selectClazz").append("<option value="+data[i].id+" selected='selected'>"+data[i].name+"</option>");
                }else{
                    $("#selectClazz").append("<option value="+data[i].id+">"+data[i].name+"</option>");
                }
            }
            var clazzId=$("#selectClazz").val();
            $.ajax({
                type : "get",
                url : "/grouping/getUser",
                dataType : "json",
                data:{
                    clazzId:clazzId
                },
                success : function(data) {
                    getUser(data);
                    console.log(data);
                },
                error : function(xhr,textStatus) {
                    console.log('错误');
                    console.log(xhr);
                    console.log(textStatus);
                }
            });
            groupUser(clazzId);
            console.log(data);
        },
        error : function(xhr,textStatus) {
            console.log('错误');
            console.log(xhr);
            console.log(textStatus);
        }
    });


    $("#selectClazz").change(function(){
        var clazzId=$("#selectClazz").val();
        $("#s1").empty();
        $("#s2").empty();
        $.ajax({
            type : "get",
            url : "/grouping/getUser",
            dataType : "json",
            data:{
                clazzId:clazzId
            },
            success : function(data) {
                getUser(data);
                $("#t1 tr:not(:first)").empty();
                groupUser(clazzId);
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });

    $("#insert").click(function(){
        var userName1=$("#s1").val();
        var userName2=$("#s2").val();
        var clazzId=$("#selectClazz").val();
        $.ajax({
            type : "get",
            url : "/grouping/addTeam",
            dataType : "json",
            data:{
                userName1:userName1,
                userName2:userName2,
                clazzId:clazzId
            },
            success : function(data) {
                if(data==1){
                    alert("插入成功");
                    $("#t1 tr:not(:first)").empty();
                    groupUser(clazzId);
                }else{
                    alert("该组员已经分组")
                }
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    });


    function groupUser(clazzId){
        $.ajax({
            type : "get",
            url : "/grouping/groupUser",
            dataType : "json",
            data:{
                clazzId:clazzId
            },
            success : function(data) {
                for(var i in data){
                    $("#t1").append("<tr>" +
                        "<td style='display: none'>" + data[i].id+ "</td>" +
                        "<td>"+(parseInt(i)+1)+"</td>" +
                        "<td>"+data[i].userName1+"</td>" +
                        "<td>"+data[i].userName2+"</td>" +
                        "<td >" +"\<input type='button' class='deleteGroup' value='删除'>\</td></tr>");
                }
                $(".deleteGroup").click(function(){
                    var id=$(this).parent().parent().find("td").eq(0).text();
                    $.ajax({
                        type : "get",
                        url : "/grouping/deleteGroup",
                        dataType : "text",
                        data:{
                            id:id
                        },
                        success : function(data) {
                            alert(data);
                            $("#t1 tr:not(:first)").empty();
                            groupUser(clazzId);
                            console.log(data);
                        },
                        error : function(xhr,textStatus) {
                            console.log('错误');
                            console.log(xhr);
                            console.log(textStatus);
                        }
                    });
                        $(this).parent().parent().remove();

                });
                console.log(data);
            },
            error : function(xhr,textStatus) {
                console.log('错误');
                console.log(xhr);
                console.log(textStatus);
            }
        });
    }

    function getUser(data){
        for(var i in data){
            if(data[i].type==2){
                continue;
            }
            if(i==0){
                $("#s1").append("<option value="+data[i].id+" selected='selected'>"+data[i].name+"</option>");
                $("#s2").append("<option value="+data[i].id+" selected='selected'>"+data[i].name+"</option>");
            }else{
                $("#s1").append("<option value="+data[i].id+">"+data[i].name+"</option>");
                $("#s2").append("<option value="+data[i].id+">"+data[i].name+"</option>");
            }
        }
    }
});